package edu.ruc.cluster.action;

import java.io.IOException;

import edu.ruc.cluster.util.AbstractInteractiveCommand;

/**
 * 2012-12-11
 * Used: install jdk from .bin file 
 * Return: installed directory under /usr/java
 * Called example: 
 * 		ExecJdkBInCmd cmd = new ExecJdkCmd("dlnode13", "deke", "111111");
 * 		String dirpath = cmd.dealcmd("/bin/file/path");
 * @author deke
 */
public class ExecJdkBinCmd extends AbstractInteractiveCommand {

	private static String jdkfolder = null;
	private static String lsinfo = null;
	private static String jdkrpm = null;
	public ExecJdkBinCmd(String host, String passwd){
		super(host, passwd);
	}
	@Override
	public void interactProcess(String cmd) {
		// TODO Auto-generated method stub
		String[] commands = cmd.split("xxx");
		try{
			int count = 1;
			for(int i = 0 ; i < commands.length ; i ++){
				writor.write((commands[i]+ "\n").getBytes());
				writor.flush();
							
		    	char[] cbuf = new char[1024];			
				int ret = reader.read(cbuf);
				
				while(ret != -1){
					String info = new String(cbuf, 0, ret);
					if(info.contains("%") == false)
						System.out.print(info);
					
					if(info.contains("package") && info.contains("is already installed")){
						jdkrpm = info.substring(0, info.indexOf("is already installed"));
						jdkrpm = jdkrpm.substring(jdkrpm.lastIndexOf("package") + 7).trim();
					}
					if(i == commands.length - 1){
						lsinfo += info;
					}
					if(info.contains("[" + getUser()+ "@" )){	
//					if(info.contains("@" + getHost())){
						if(count == 0){
							break;
						}
						count --;
					}else if(info.contains("password")){
						writor.write((getPasswd() + "\n").getBytes());
						writor.flush();
					}
//					else if(info.contains("Press Enter to continue..."))
//						break;
					
					ret = reader.read(cbuf);
				}
			}
		}catch(IOException e){
			logHandler.error(this.getClass().getSimpleName() + e.getMessage(), e);
		}
	}
	public String install(String binPath){
		String cmd1 = "chmod +x " + binPath;
		String cmd2 =  "yes | sudo " + binPath;
		//Get installed jdk information
		String cmd3 = "ls -t /usr/java | grep jdk";
		dealcmd(cmd1 + "xxx" + cmd2 + "xxx" + cmd3);
		
		lsinfo = lsinfo.replace(cmd3, "");
//		System.out.println("jdkrpm: " + jdkrpm);
//		System.out.println("ls info: " + lsinfo);
		
		if(jdkrpm != null){
			for(String str : lsinfo.split("\\s")){
				if(str.contains("jdk") && jdkrpm.contains(str.replace("jdk", ""))){
					jdkfolder = str;
					break;
				}
			}
		}else{
			for(String str : lsinfo.split("\\s")){
				System.out.println(str);
				if(str.contains("jdk")){
					jdkfolder = str;
					break;
				}
			}
		}
		if(jdkfolder != null)
			logHandler.info("returned jdkforlder name : " + jdkfolder);
		else{
			throw new NullPointerException();
		}
		return jdkfolder;
	}
	
	/*
	public static void main(String[] args){
		String hosts = "deke@dlnode13";
		String passwd = "111111";
//		String filepath = "/home/deke/jdk/jdk-6u38-ea-bin-b04-linux-amd64-31_oct_2012-rpm.bin";
		String filepath = "/home/deke/jdk/abc.bin";
		ExecJdkBinCmd ejbc = new ExecJdkBinCmd(hosts, passwd);
		String folder = ejbc.install(filepath);
		System.out.println(folder);
	}
	*/
	
}
